package com.tanhua.server.controller;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementController {
    @Autowired
    private MovementService movementService;

    @Autowired
    private CommentService commentService;

    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile imageContent[]) throws IOException {
        movementService.publishMovement(movement, imageContent);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/all")
    public ResponseEntity all(Long userId,
                              @RequestParam(defaultValue = "1") int page,
                              @RequestParam(defaultValue = "10") int pagesize) {
        PageResult pr = movementService.findByUserId(userId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") int page,
                                    @RequestParam(defaultValue = "10") int pagesize) {
        PageResult pr = movementService.findFriendMovements(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") int page,
                                    @RequestParam(defaultValue = "10") int pagesize) {
        PageResult pr = movementService.findRecommendMovements(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    @GetMapping("{id}")
    public ResponseEntity findById(@PathVariable String id) {
        MovementsVo vo = movementService.findById(id);
        return ResponseEntity.ok(vo);
    }

    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable String id) {
        Integer likeCounts = commentService.like(id);
        return ResponseEntity.ok(likeCounts);
    }

    @GetMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable String id) {
        Integer likeCounts = commentService.disLike(id);
        return ResponseEntity.ok(likeCounts);
    }

    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable String id) {
        Integer likeCounts = commentService.love(id);
        return ResponseEntity.ok(likeCounts);
    }

    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable String id) {
        Integer likeCounts = commentService.unlove(id);
        return ResponseEntity.ok(likeCounts);
    }
    //首页显示谁访问过我
    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        List<VisitorsVo> list=movementService.visitors();
        return ResponseEntity.ok(list);
    }
}

